﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kovan.Model.Parameter.Config;
using Kovan.BLL.Config;
using Kovan.Model.Entity.Config;
using Kovan.Library.Configuration;
using Kovan.Library.IO;

namespace Kovan.UI.Areas.Manage.Controllers
{
    public class LinkController : ManageControllerBase
    {
        //
        // GET: /Manage/Link/

        public ActionResult Index(PLink param)
        {
            if (!(param.PageSize > 0))
            {
                param.PageSize = 20;
            }
            if (!(param.PageIndex > 0))
            {
                param.PageIndex = 1;
            }
            var items = BLink.GetInstances().GetList(param);
            ViewBag.Param = param;
            return View("index", items);
        }

        public ActionResult GetOperate(int? id)
        {
            tb_link model = new tb_link();
            if (id.HasValue)
            {
                model = BLink.GetInstances().GetModel(id.Value);
            }
            return View("OperateDialog", model);
        }

        public ContentResult Delete(string ids)
        {
            var statuscode = string.Empty;
            List<string> idss = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            var result = BLink.GetInstances().BatchDelete(idss);
            if (result > 0)
            {
                statuscode = GetSucceedCode("操作成功", "link_manage", Url.Content("~/Manage/Link"), "友情链接");
            }
            return Content(statuscode);
        }

        public ActionResult SaveLink(tb_link model)
        {
            int result = 0;
            string statuscode = string.Empty;
            if (model.link_id.HasValue)
            {
                var model1 = BLink.GetInstances().GetModel(model.link_id.Value);
                model1.title = model.title;
                model1.description = model.description;
                model1.img_path = model.img_path;
                model1.is_index = model.is_index;
                model1.is_view = model.is_view;
                model1.link_target = model.link_target;
                model1.sort_order = model.sort_order;
                model1.url = model.url;

                result = BLink.GetInstances().Update(model1);
            }
            else
            {
                model.application_id = 1;
                model.create_date = DateTime.Now;

                result = BLink.GetInstances().Add(model);
            }

            if (result > 0)
            {
                if (!string.IsNullOrEmpty(model.img_path) && model.img_path.ToLower().Trim() != "folder.gif")
                {
                    string sourePath = Server.MapPath("~/" + ConfigurationUtility.AppSettings<string>("PictureTemp", "content/Uploads/Temp/") + model.img_path);
                    var targetPath = Server.MapPath("~/" + ConfigurationUtility.AppSettings<string>("LinkPic", "content/Uploads/Link/") + model.img_path);

                    FileUtility.MoveFile(sourePath, targetPath, true);
                }
                //成功
                statuscode = GetSucceedCode("保存成功", "link_manage", Url.Content("~/Manage/Link"), "友情链接");
            }
            else
            {
                //失败
                statuscode = GetFailCode("保存失败", "", "", "");
            }
            return Content(statuscode);
        }

        public ActionResult UploadDialog()
        {
            return PartialView("UploadDialog");
        }

    }
}
